package com.restfb.etl.tareas;

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import com.restfb.etl.mundo.Comment;
import com.restfb.etl.mundo.CommentListado;
import com.restfb.etl.mundo.Message;



public class TareaCreacionCVSUserResponses 
{


	List<Message> mensajes;
	public static final String RUTA="./data/otros/userresponses.csv";
	public TareaCreacionCVSUserResponses(List<Message> nMessage)
	{

		mensajes=nMessage;

	}

	public List<String> execute()
	{	List<String> idUsuarios=new ArrayList<String>();
		try {
			PrintWriter pw=new PrintWriter(new File(RUTA));
			//Iprimir encabezados
			pw.println("ID_ACTOR,CAMPANA_NOMBRE,CAMPANA_DESCRIPCION,id,time,contenido_reaccion,numero_likes,from_id");
			for(Message men:mensajes)
			{
				CommentListado listaCom=men.comments;
				List<Comment> comentarios=listaCom.comment_list;
				for(Comment ls:comentarios)
				{
					idUsuarios.add(""+ls.fromid);
					pw.println(men.actor_id+","+men.post_id+","+men.message.trim().replace("\n", "").replace(",", "_")+","+ls.id+","+ls.time+","+ls.text.trim().replace("\n", "").replace(",", "_")+","+ls.likes+","+ls.fromid);
				}
				
				
				
			}
			
			pw.close();
//			for(Message men:mensajes)
//			{
//				//{"count":4,"comment_list":[{"id":"72345815262_10150577036015263_20704412","user_likes":false,"time":1304379803,"text":"lo mas justo es que por ciudad den un premio ,,asi se evitan premiar  y permitir que esos personajillos  sigan defraudando las empresas","likes":0,"fromid":100001009232735},{"id":"72345815262_10150577036015263_20705905","user_likes":false,"time":1304382830,"text":"!!!Despues no digas que no te avisamos!!!! jajjajajaa","likes":0,"fromid":100001011363932}],"can_post":true,"can_remove":false}
//
//				//[{"id":"72345815262_211697812175517_3091157","user_likes":false,"time":1302129854,"text":"SI , SALUDITOS  A CALI ....... FELIZ VIAJE...","likes":0,"fromid":1168778108},{"id":"72345815262_211697812175517_3102334","user_likes":false,"time":1302298665,"text":"Exitos","likes":0,"fromid":1279710334}]
//				String com=men.comments.replace("[", "%").replace("]","%");
//				String[] comment=com.split("%");
//				String[] commentDoble=com.split("%%");
//				System.out.println("Comentario!!"+com);
//				if(commentDoble.length!=2)
//				{
//					
//					
//					
//					//{"id":"72345815262_211697812175517_3091157","user_likes":false,"time":1302129854,"text":"SI , SALUDITOS  A CALI ....... FELIZ VIAJE...","likes":0,"fromid":1168778108},{"id":"72345815262_211697812175517_3102334","user_likes":false,"time":1302298665,"text":"Exitos","likes":0,"fromid":1279710334}
//					String comentarios=comment[1];
//
//					System.out.println("Aqui esta!"+comentarios);
//					String[] comentarioSplit=comentarios.replace("},{", "%").split("%");
//					for (int i = 0; i < comentarioSplit.length; i++) 
//					{
//						if(!comentarios.equals(""))
//						{
//						//cada uno es un comentario
//						String comentTemp=comentarioSplit[i];
//						System.out.println("Texto!"+comentTemp);
//						comentTemp=comentTemp.replace("{", " ").replace("}", " ").trim();
//						String[] datos=comentTemp.replace("\":", "%").split("%");
//
//						//Quitar comillas
//						String id=datos[1].replace("\",\"user_likes"," ").replace("\"", " ").trim();
//
//
//						String userlikes=datos[2].replace(",\"time", " ").trim();
//						String time=datos[3].replace(",\"text", " ").trim();
//						//Quitar comillas
//						String text=datos[4].replace("\",\"likes", " ").trim();
//						
//						String likes=datos[5].replace(",\"fromid"," ").trim();
//						String formId=datos[6].replace("{"," ").replace("}"," ").trim();
//
//						System.out.println(id+"\n"+userlikes+"\n"+time+"\n"+text+"\n"+likes+"\n"+formId);
//						
//						pw.println(men.actor_id+","+men.post_id+","+men.message.trim().replace("\"n", " ").replace(",", "_")+","+id+","+userlikes+","+time+","+text.trim().replace("\"n", " ").replace(",", "_")+","+likes+","+formId);
//						
//						}
//					}
//
//					
//
//					
//				}
//				
//			}
			
			
			
			
			
			

		}
		catch(Exception e)
		{

			System.err.println("Error guardando el archivo csv de user responses");
			e.printStackTrace();
		}
		
		return idUsuarios;
	}
}
